import time

# 导入token验证函数
from common.token_dec import *
from django.core.paginator import Paginator  # 这个是处理分页的
from django.forms import model_to_dict  # 这个是转字典的，要是不导入这个的话，接口返回json的时候会报错
# 导入类视图
from django.views import View
# 导入商品处理函数
from common.goods.get_goods import *
# 导入响应数据函数
from common.respond.res import res


# Create your views here.


# 获取热门搜索关键词
class GetHotSea(View):
    def get(self, request):
        hot_sea = list(GoodsSeaHot.objects.all().values())
        # 获取全部数据
        return res(200, "获取成功！", {"goodsHotSea": hot_sea})


# 获取所有商品
class GetALlGoods(View):
    def get(self, request):
        # 获取所有商品
        goods_list = get_goods()
        # 获取全部数据
        return res(200, "获取成功！", {"goodsList": goods_list})


# 搜索商品
class SeaGoods(View):
    def get(self, request):
        page = request.GET.get('page', None)  # 获取第几页
        limit = request.GET.get('limit', None)  # 每页有多少条数据
        name = request.GET.get('name', None)  # 每页有多少条数据
        # 设置默认值
        if page is None or page == '':
            page = 1
        if limit is None or limit == '':
            limit = 5
        # 获取所有商品
        goods_list = Goods.objects.filter(goods_name__contains=name)
        paginator = Paginator(goods_list, limit)
        page_1 = paginator.get_page(page)
        data_liat = []
        for i in page_1:
            mode_to = model_to_dict(i)
            data_liat.append(mode_to)

        return res(200, "获取成功！", {"goodsList": data_liat, 'count': paginator.count})


# 获取商品推荐商品
class HomePushGoods(View):
    def get(self, request, push_id):
        page = request.GET.get('page', None)  # 获取第几页
        limit = request.GET.get('limit', None)  # 每页有多少条数据
        # 设置默认值
        if page is None or page == '':
            page = 1
        if limit is None or limit == '':
            limit = 5
        # 获取指定推送的商品
        goods_list = Goods.objects.filter(goods_pushId=push_id)
        # 对数据进行分页处理
        paginator = Paginator(goods_list, limit)
        page_1 = paginator.get_page(page)
        # 获取到分页的数据
        data_liat = []
        for i in page_1:
            mode_to = model_to_dict(i)
            # 将商品数据进行完善
            item = goods_item(mode_to)
            data_liat.append(item)
        return res(200, "获取成功！", {"list": data_liat, 'count': paginator.count, 'time': int(time.time())})


# 获取超值爆款商品
class HomeHotGoods(View):
    def get(self, request, type_id):
        page = request.GET.get('page', None)  # 获取第几页
        limit = request.GET.get('limit', None)  # 每页有多少条数据
        # 设置默认值
        if page is None or page == '':
            page = 1
        if limit is None or limit == '':
            limit = 5
        # 获取指定推送的商品
        goods_list = Goods.objects.filter(goods_signId=type_id)
        # 对数据进行分页处理
        paginator = Paginator(goods_list, limit)
        page_1 = paginator.get_page(page)
        # 获取到分页的数据
        data_liat = []
        for i in page_1:
            mode_to = model_to_dict(i)
            # 将商品数据进行完善
            item = goods_item(mode_to)
            data_liat.append(item)
        return res(200, "获取成功！", {"list": data_liat, 'count': paginator.count, 'time': int(time.time())})


# 获取一级分类商品
class HomeCateGoods(View):
    def get(self, request, nav_id):
        page = request.GET.get('page', None)  # 获取第几页
        limit = request.GET.get('limit', None)  # 每页有多少条数据
        # 设置默认值
        if page is None or page == '':
            page = 1
        if limit is None or limit == '':
            limit = 5
        # 获取指定推送的商品
        goods_list = Goods.objects.filter(goods_navId=nav_id)
        # 对数据进行分页处理
        paginator = Paginator(goods_list, limit)
        page_1 = paginator.get_page(page)
        # 获取到分页的数据
        data_liat = []
        for i in page_1:
            mode_to = model_to_dict(i)
            # 将商品数据进行完善
            item = goods_item(mode_to)
            data_liat.append(item)
        return res(200, "获取成功！", {"list": data_liat, 'count': paginator.count, 'time': int(time.time())})


# 获取三级商品
def good_sid_list(request):
    page = request.GET.get('page', None)  # 获取第几页
    limit = request.GET.get('limit', None)  # 每页有多少条数据
    sid = request.GET.get('sid', None)
    # 获取指定推送的商品
    goods_list = Goods.objects.filter(goods_sid=sid)
    # 对数据进行分页处理
    paginator = Paginator(goods_list, limit)
    page_1 = paginator.get_page(page)
    data_liat = []
    for i in page_1:
        mode_to = model_to_dict(i)
        # 将商品数据进行完善
        item = goods_item(mode_to)
        data_liat.append(item)
    return res(200, "获取成功！", {"list": data_liat, 'count': paginator.count, 'time': int(time.time())})


# 获取单个商品数据
class GetGoodsDetail(View):
    def get(self, request):
        # 获取商品id
        goods_data = request.GET
        # 根据id获取商品信息
        goods = Goods.objects.filter(id=goods_data['id'], goods_id=goods_data['goods_id']).values()
        goods_dict = {}
        # 将商品信息补全
        for i in goods:
            item = goods_item(i)
            goods_dict = item
        return res(200, "获取成功！", {'data': goods_dict, 'time': int(time.time())})


@token_dec
# 收藏商品
def sc_goods(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 商品id
    goods_id = request.GET.get("goodsId")
    # 将商品放入收藏表
    GoodsCollect(uid=user_id, gid=goods_id).save()
    return res(200, "收藏成功！", {'time': int(time.time())})


@token_dec
# 取消收藏
def qx_goods(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 商品id
    goods_id = request.GET.get("goodsId")
    # 删除收藏的商品
    GoodsCollect.objects.filter(uid=user_id, gid=goods_id).delete()
    return res(200, "取消成功！", {'time': int(time.time())})


@token_dec
# 获取收藏的商品
def get_collect_goods(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 获取所有收藏的商品
    collect_list = list(GoodsCollect.objects.filter(uid=user_id).values())
    # 商品列表
    goods_list = []
    # 将商品id取出来
    for cart in collect_list:
        # 遍历商品id获取所有商品
        goods = Goods.objects.filter(goods_id=cart['gid']).values()
        # 将商品信息补全
        for i in goods:
            item = goods_item(i)
            goods_list.append(item)
    return res(200, "获取成功！", {"collectList": goods_list, 'time': int(time.time())})


@token_dec
# 加入购物车
def add_cart(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 商品id
    goods_id = request.GET.get("goodsId")
    # 判断是否加入过购物车
    flag = GoodsCart.objects.filter(uid=user_id, gid=goods_id).exists()
    if flag:
        return res(400, "加入失败！", {'err': "该商品已经加入过购物车！"})
    # 将商品放入收藏表
    GoodsCart(uid=user_id, gid=goods_id).save()
    return res(200, "加入成功！", {'time': int(time.time())})


@token_dec
# 移出购物车
def delete_cart(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 商品id
    goods_id = request.GET.get("goodsId")
    # 移除存在的购物车商品
    flag = GoodsCart.objects.filter(uid=user_id, gid=goods_id).exists()
    if flag:
        # 删除收藏的商品
        GoodsCart.objects.filter(uid=user_id, gid=goods_id).delete()
        return res(200, "移除成功！", {'time': int(time.time())})
    else:
        return res(400, "移除失败！", {'err': "购物车没有该商品"})


@token_dec
# 获取购物车列表
def get_all_cart(request):
    # 获取当前token用户id
    user_id = request.myuser['userId']
    # 获取所有收藏的商品
    cart_list = list(GoodsCart.objects.filter(uid=user_id).values())
    # 商品列表
    goods_list = []
    # 将商品id取出来
    for cart in cart_list:
        # 遍历商品id获取所有商品
        goods = Goods.objects.filter(goods_id=cart['gid']).values()
        # 将商品信息补全
        for i in goods:
            i['num'] = cart['num']
            item = goods_item(i)
            goods_list.append(item)
    return res(200, "获取成功！", {"goodsCart": goods_list, 'time': int(time.time())})

# 获取三级商品
